package com.ruoyi.project.setting.security.service;

import com.dyun.tool.jwt.service.DyPermService;
import com.ruoyi.project.setting.security.LoginUser;
import com.ruoyi.project.setting.security.utils.UserUtils;
import com.ruoyi.common.enums.BaseStatusEnum;
import com.ruoyi.project.system.entity.SysMenu;
import com.ruoyi.project.system.entity.SysRole;
import com.ruoyi.project.system.service.ISysMenuService;
import com.ruoyi.project.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户权限处理
 *
 * @author ruoyi
 */
@Component
public class SysPermissionService implements DyPermService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取角色数据权限
     */
    @Override
    public Set<String> getRoles() {
        LoginUser loginUser = UserUtils.currentLoginUser();
        return getSetRoles(loginUser);
    }

    public Set<String> getSetRoles(LoginUser loginUser) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (!CollectionUtils.isEmpty(loginUser.getRoles())) {
            return loginUser.getRoles();
        }
        if (UserUtils.isAdmin(loginUser.getUserId())) {
            roles.add("admin");
        }
        List<SysRole> roleList = roleService.listUserRoleByUserId(loginUser.getUserId());
        // 未停用角色
        roles.addAll(roleList.stream().filter(r -> BaseStatusEnum.DB_STATUS_NORMAL.getStatus().equals(r.getStatus())).map(SysRole::getRoleKey).collect(Collectors.toSet()));

        // 用户所有角色及所有未停用角色标识
        loginUser.setRoleList(roleList);
        loginUser.setRoles(roles);
        return roles;
    }

    /**
     * 获取菜单数据权限
     */
    @Override
    public Set<String> getPermissions() {
        LoginUser loginUser = UserUtils.currentLoginUser();
        return getSetPermissions(loginUser);
    }

    public Set<String> getSetPermissions(LoginUser loginUser) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (!CollectionUtils.isEmpty(loginUser.getPermissions())) {
            return loginUser.getPermissions();
        }
        if (UserUtils.isAdmin(loginUser.getUserId())) {
            perms.add("*:*:*");
        }
        // 确保先获取角色
        if (CollectionUtils.isEmpty(loginUser.getRoleList())) {
            getSetRoles(loginUser);
        }
        List<SysRole> roleList = loginUser.getRoleList();
        if (!CollectionUtils.isEmpty(roleList)) {
            // 未停用角色
            List<Long> roleIds = roleList.stream().filter(r -> BaseStatusEnum.DB_STATUS_NORMAL.getStatus().equals(r.getStatus())).map(SysRole::getId).collect(Collectors.toList());

            List<SysMenu> menuList = menuService.listUserMenuByRoles(roleIds);
            // 用户所有权限及所有未停用权限标识
            perms.addAll(menuList.stream().filter(m -> !BaseStatusEnum.MENU_STATUS_DISABLE.getStatus().equals(m.getStatus())).map(SysMenu::getPerms).map(String::trim).collect(Collectors.toSet()));
            loginUser.setResourceList(menuList);
        }

        loginUser.setPermissions(perms);
        return perms;
    }
}
